<?php
class upload_library{
	function __construct()
	{
	  $this->CI = get_instance();
	}
    function check_img_ext($ext){
        $arr_ext = array('jpg','png','gif','jpeg');
        if(in_array($ext,$arr_ext)){
            return true;
        }else{
            return false;
        }
    }
	function uploadadv($name)
	{
		if(is_uploaded_file($_FILES['filedata']['tmp_name']))
		{
			$filename = stripslashes($_FILES['filedata']['name']);
			$extension = strtolower($this->file_extension($filename));                
			$filename = '../khuyenmai/uploads/quangcao/'.$name.date("dmYHis").'.'.$extension;
			if(move_uploaded_file($_FILES['filedata']['tmp_name'], $filename)){
				return $filename;
			}else{
					return $filename='';  
			}              
		}
		else
		{
			return $filename=''; 
		}
	}
	
	function uploadfile()
	{
		if(is_uploaded_file($_FILES['filedata']['tmp_name']))
		{
			$filename = stripslashes($_FILES['filedata']['name']);
			$size = $_FILES["filedata"]["size"];
             
			$extension = strtolower($this->file_extension($filename)); 
			$name = date("dmYHis").'.'.$extension ;          
			$filename = '../uploads/images/'. $name ;
			$filedata = 'uploads/images/'. $name ;
			if(move_uploaded_file($_FILES['filedata']['tmp_name'], $filename)){
				$size = ($size/1024);
				return $filedata.'*'.$size.'*'.$extension.'*'.$name;
			}else{
				return $filedata='';  
			}              
		}else{
			return $filedata=''; 
		}
	}
    function upload_file_to_dir($destination){
        $arr_file = array();
        $arr_file['err']='';
        if(is_uploaded_file($_FILES['filedata']['tmp_name']))
		{
			$filename = stripslashes($_FILES['filedata']['name']);
			$size = $_FILES["filedata"]["size"];
             
            if($size>0){
                $ext = strtolower($this->file_extension($filename)); 
                if($this->check_img_ext($ext))
                {
                    //$name = date("dmYHis").'.'.$ext ;  
					$name_temp = str_replace(".$ext",'',$filename);
					$name = stringchange($name_temp).'_'.rand().'.'.$ext;
                    $filename = $destination.$name ;
                    if(move_uploaded_file($_FILES['filedata']['tmp_name'], $filename)){
                        $size = ($size/1024);
                        $arr_file['size']=$size;
                        $arr_file['ext']=$ext;
                        $arr_file['name']=$name;

                    }else{
                        $arr_file['err']='Chuyển file không thành công, vui lòng thử lại';
                    }
                }else{
                    $arr_file['err']='Kiểu tập tin không hợp lệ, kiểu hợp lệ phải là jpg,gif,png,jpeg';
                }
            }else{
                $arr_file['err']='Kích thước file quá nhỏ';
            }
		}else{
            $arr_file['err']='Tập tin upload không tồn tại, vui lòng thử lại';
        }
        return $arr_file;
    }
    function uploadfile_new($dir_path)
	{
            if(is_uploaded_file($_FILES['filedata']['tmp_name']))
            {
                    $filename = stripslashes($_FILES['filedata']['name']);
                    $size = $_FILES["filedata"]["size"];

                    $extension = strtolower($this->file_extension($filename)); 
                    $name = date("dmYHis").'.'.$extension ;          
                    $filename = '../'.$dir_path.'/'. $name ;
                    $filedata = $dir_path.'/'. $name ;
                    if(move_uploaded_file($_FILES['filedata']['tmp_name'], $filename)){
                            $size = ($size/1024);
                            return $filedata.'*'.$size.'*'.$extension.'*'.$name;
                    }else{
                            return $filedata='';  
                    }              
            }else{
                    return $filedata=''; 
            }
	} 
	function file_extension($filename)
	{
		$path_info = pathinfo($filename); 
		return $path_info['extension'];
	}    
	
}  
?>
